import ModelBase from '..'
import { get } from '../../../utils/request'

function BookModel() {}

BookModel.prototype = new ModelBase('Book', {
  saveUrl: function(userId) {
    return `/users/${userId}/book/${this.id || 0}`
  },
})

/**
 * 根据用户Id查询书本集合
 * @param {*} userId
 */
BookModel.prototype.getBooksByUserId = async function(userId) {
  const res = await get(`/users/${userId}/books`)
  const books = (res && res.data) || []
  return books.map((book) => {
    const data = this.push(book)
    return data
  })
}

export default new BookModel()
